//__________________________________选择单返程_______________________________________	
	$(".route input[type=radio]").click(function(){
			if(!$(this).hasClass("checked-hook")){
				$(".flightList").empty();			
				if($(this).val()=="OW"){			
					for(var i=0;i<1;i++){
						addHangCheng()
					}					
				}else if($(this).val()=="RT"){
					for(var i=0;i<2;i++){
						addHangCheng()
					}					
				}else{
					for(var i=1;i<3;i++){
						addMWHangCheng(i)
					}				
				}
				paiqixianshi()//排期的显示隐藏
				$(this).addClass("checked-hook").siblings().removeClass("checked-hook");
			}							
	})
	//排期的显示
	function paiqixianshi(){
		var value = $(".route input[type=radio]:checked").val();
		if(value=="OW"){
			$(".scheduleTitle").css("display","none");//单程无排期
			$(".schedule").css("display","none");						
		}else if(value=="RT"){
			$(".scheduleTitle").css("display","block");//单程无排期
			$(".schedule").css("display","block");
			$(".asInterval").attr("disabled",false);
			$(".autoRuleInput").attr("disabled",false);
			$(".autoRuleInput").prop("checked",true);
		}else{
			$(".scheduleTitle").css("display","block");//单程无排期
			$(".schedule").css("display","block");
			$(".asWeek").prop("checked",true);
			$(".asInterval").attr("disabled",true);
			$(".autoRuleInput").attr("disabled",true);
			$(".scheduleTable_1").show();
			$(".addSchedule").show();
			$(".scheduleTable_2").hide();
			$(".scheduleTable_3").hide();
		}
	}


//_________________________________点击添加 选择航班_________________________________________
	var flightTableIndex
$(".flightList").delegate(".add","click",function(){
	flightTableIndex = $(this).attr("index");
	console.log(flightTableIndex)
	layer.open({
		type: 2,
		title: ['选择航班','background-color:#6d6e72; color:#fff;font-size:14px;'],
		shadeClose: true,
		shade: [0.9,'#cfcfcf'],
		area: ['100%', '100%'],  
		content: ['../ak/chooseFlight.html'],// iframe的url，no代表不显示滚动条
		end:function(){
			
		}			
	});
})
//_________________________________点击添加  选择运价方案_________________________________________
$(".pricePlan").delegate(".addPricePlan","click",function(){
	layer.open({
		type: 2,
		title: ['选择运价方案','background-color:#6d6e72; color:#fff;'],
		shadeClose: true,
		shade: [0.9,'#cfcfcf'],
		area: ['100%', '100%'],  
		content: ['../ak/choosePricePlan.html'],// iframe的url，no代表不显示滚动条
		end:function(){
			
		}			
	});
})
//_________________________________点击添加  选择免票运价方案_________________________________________
$(".pricePlan").delegate(".addFreePricePlan","click",function(){
	layer.open({
		type: 2,
		title: ['选择免票运价方案','background-color:#6d6e72; color:#fff;'],
		shadeClose: true,
		shade: [0.9,'#cfcfcf'],
		area: ['100%', '100%'],  
		content: ['../ak/choosePricePlan.html?free=free'],// iframe的url，no代表不显示滚动条
		end:function(){
			
		}			
	});
})
//_________________________________点击添加航程_________________________________________
$(".addFlightRoute").click(function(){
	addHangCheng()	
})
	function addHangCheng(){
		var index = $(".flight").length;
		var length = $(".flight").length+1
		var str="<div class='flight'>"+
					"<div class='flight_Content f868686'>"+
						"<p>"+
							"<span class='smallTitle'> 第 "+length+" 程航班</span>"+
							"<span class='do'><i index="+index+" class='add iconfont faa000' title='添加'>&#xe661; </i>"+
						"</p>"+
					"</div>"+
				"</div>"
		$(".flightList").append(str)
	}
	function addMWHangCheng(i){		
		for(var j=1;j<3;j++){
			var index = $(".flight").length;
			var str="<div class='flight'>"+
						"<div class='flight_Content f868686'>"+
							"<p>"+
								"<span class='smallTitle'> 第"+i+"程 第"+j+"段航班</span>"+
								"<span class='do'><i index="+index+" class='add iconfont faa000' title='添加'>&#xe661; </i>"+
							"</p>"+
						"</div>"+
					"</div>"
			if(j==2){
				var selectClass = "";
				if(i==1){
					selectClass += "fi";
				}
				if(i==2){
					selectClass += "si";
				}
				str+="<div class='MWinterval'>选择间隔天数<select class="+selectClass+">" +
							"<option value='0'>0</option>" +
							"<option value='1'>1</option>" +
							"<option value='2'>2</option>" +
							"<option value='3'>3</option>" +
							"<option value='4'>4</option>" +
							"<option value='5'>5</option>" +
							"<option value='6'>6</option>" +
							"<option value='7'>7</option>" +
						"</select>" +
					 "<div>"
			}
			$(".flightList").append(str)
		}
		
		
	}


//____________________________________取消按钮_______________________________________
$(".cancle").click(function(){
	var index = parent.layer.getFrameIndex(window.name);//获取弹窗索引
	parent.layer.close(index);//关闭弹窗
})

//____________________________________选择航班页面调用的父级页面的方法________________________

function setData(data){ 
	creatTableOrNot();         //判断是否生成table
	var lengthOld = $(".flight_Content").eq(flightTableIndex).find("table tr").length;//获得已有的tr个数
	for(var i=0;i<data.length;i++){   //判断生成多少个tr
		creatTr()		
	}
	var lengthNew = $(".flight_Content").eq(flightTableIndex).find("table tr").length;//获得所有的tr个数
	var dataIndex = 0;//数据数组的索引
	for(var j=lengthOld;j<lengthNew;j++){	
		$(".flight_Content").eq(flightTableIndex).find("table tr").eq(j).find("td").eq(0).text(data[dataIndex].flightNo)
		$(".flight_Content").eq(flightTableIndex).find("table tr").eq(j).find("td").eq(1).text(data[dataIndex].depAirport)
		$(".flight_Content").eq(flightTableIndex).find("table tr").eq(j).find("td").eq(2).text(data[dataIndex].arrAirport)
		$(".flight_Content").eq(flightTableIndex).find("table tr").eq(j).find("td").eq(3).text(data[dataIndex].DepDate)
		$(".flight_Content").eq(flightTableIndex).find("table tr").eq(j).find("td").eq(4).text(data[dataIndex].ArrDate)
		$(".flight_Content").eq(flightTableIndex).find("table tr").eq(j).find("td").eq(5).text(data[dataIndex].depTime)
		$(".flight_Content").eq(flightTableIndex).find("table tr").eq(j).find("td").eq(6).text(data[dataIndex].arrTime)
		$(".flight_Content").eq(flightTableIndex).find("table tr").eq(j).find("td").eq(7).text(data[dataIndex].dayofweek)	
		$(".flight_Content").eq(flightTableIndex).find("table tr").eq(j).find("td").eq(8).text(data[dataIndex].id)	
		dataIndex++
	}
	hideOthersTr();                   //隐藏除了前2个tr外的其他tr;
}
//____________________________________选择运价方案页面调用的父级页面的方法________________________

function choosePlan(data){
	$(".pricePlanNo").text(data)
}
//____________________________________选择运价方案2页面调用的父级页面的方法________________________

function choosePlanFree(data){
	$(".freePricePlanNo").text(data)
}
//____________________________________新建产品提交________________________
$(".sure").click(function(){
	var submitMess = {};	
	submitMess.productItineraries = getFlightNo();
	submitMess.type =$(".route input[type=radio]:checked").val();	
	if($(".route input[type=radio]:checked").val()=="OW"){
		if(submitMess.productItineraries.length==0){
			layer.msg("请添加航班！",{icon:2,time:1000})
			return false;
		}
		
	}
	if($(".route input[type=radio]:checked").val()=="MW"){
		submitMess.si = $(".MWinterval .si").val();
		submitMess.fi = $(".MWinterval .fi").val();
		submitMess.schType = $(".scheduleTitle input[type='radio']:checked").val();
		submitMess.productSchedules = [];
		if(submitMess.schType=="wk"){			
			var len = $(".scheduleNo").length;
			for(var i=0;i<len;i++){
				var json={};
				json.itval = $(".scheduleTable_1 .itval").eq(i).val();				
				json.nextData = $(".scheduleTable_1 .nextDate").eq(i).val();
				json.thisDate = $(".scheduleTable_1 .thisDate").eq(i).val();
				submitMess.productSchedules.push(json)
			}
		}		
	}
	if($(".route input[type=radio]:checked").val()=="RT"){	
		if(submitMess.productItineraries.length!=2){
			layer.msg("请添加完整的航班信息！",{icon:2,time:1000})
			return false;
		}
		
		var index
		submitMess.schType = $(".scheduleTitle input[type='radio']:checked").val();
		if(submitMess.schType==undefined){
			layer.msg("请选择排期类型！",{icon:2,time:1000})
			return false;
		}
		submitMess.productSchedules = [];
		if(submitMess.schType=="wk"){
			var len = $(".scheduleNo").length;
			for(var i=0;i<len;i++){
				var json={};
				json.itval = $(".scheduleTable_1 .itval").eq(i).val();				
				json.nextData = $(".scheduleTable_1 .nextDate").eq(i).val();
				json.thisDate = $(".scheduleTable_1 .thisDate").eq(i).val();
				submitMess.productSchedules.push(json)
			}
		}		
		if(submitMess.schType=="iv"){
			var json = {};
			json.itval = $(".itval_").val();
			json.nextData = $(".nextDate_").val();
			json.thisDate = $(".thisDate_").val();
			if(json.itval==""||json.nextData==""||json.thisDate==""){
				layer.msg("请添加完整的组合排期！",{icon:2,time:1000})
				return false;
			}
			submitMess.productSchedules.push(json)
		}if(submitMess.schType=="AUTO"){
			var autoRule = $(".autoRule").val();	
			var json = {};
			json.itval = "";
			json.nextData = "";
			json.thisDate =  autoRule.split("/");
			
			if(autoRule==""){
				layer.msg("自动排期不能为空",{icon:2,time:1000})
				return false;
			}	
			submitMess.productSchedules.push(json)
		}
		
	}
	//联运信息
	if($(".conInput")){
		if($(".conInput").val()==""){
			layer.msg("请填写联运信息",{icon:2,time:1000})
			return false;
		}else{
			submitMess.connectFltInfo = $(".conInput").val();
		}
	}
	//升舱信息
	if($(".upSeatInput")){
		if($(".upSeatInput").val()==""){
			layer.msg("请填写升舱信息",{icon:2,time:1000})
			return false;
		}else{
			submitMess.seatUp = $(".upSeatInput").val();
		}
	}
	//运价信息
	if($(".pricePlanNo").text()==""){		
		layer.msg("请选择运价方案",{icon:2,time:1000})
		return false;
	}else{
		submitMess.fareId = $(".pricePlanNo").text();
	}
	submitMess.freeFareId = $(".freePricePlanNo").text();
	layer.load();
	$.ajax({
		type:"post",
		url:"../product?temp="+Math.random(),
		contentType:'application/json;charset=utf-8',
		async:true,
		data:JSON.stringify(submitMess),
		success:function(data){
			layer.closeAll('loading');
			data = JSON.parse(data)
			if(data.status=="1001"){
				layer.msg('新建产品成功', {icon: 1,time:1000},function(){
					var index = parent.layer.getFrameIndex(window.name);//获取弹窗索引
					parent.layer.close(index);//关闭弹窗
				})
			}if(data.status=="1002"){
				/*layer.msg('新建产品失败,'+data.message, {icon: 2,time:2000},function(){
					return false;
				})*/
				//失败的弹出框
				layer.confirm(data.message, {
					title:['新建产品失败','background-color:#6d6e72;color:#fff;font-size:14px;'],
				   	btn: ['确定'] //按钮
				}, function(){
					layer.closeAll('dialog');
					return false;
				});
			}
							
		}			
	});	
	
})
//______________________________________________________________________________添加升舱信息
	$(".addUpSeat").click(function(){
		var str="";
		if($(".upSeatMess").length==0){
			str+="<p class='upSeatMess f868686'>升舱  <input class='upSeatInput' type='text' placeholder='请输入升舱信息'/><span class='removeUs'>删除</span></p>"
			$(".con").append(str)
		}
		
	})
//______________________________________________________________________________删除升舱信息
	$(".con").delegate(".removeUs","click",function(){
		$(".upSeatMess").remove();
	})

//______________________________________________________________________________添加联程信息
	$(".addConnect").click(function(){
		var str="";
		if($(".connectMess").length==0){
			str+="<p class='connectMess f868686'>联运  <input class='conInput' type='text' placeholder='请输入联程信息'/><span class='removeCon'>删除</span></p>"
			$(".con").append(str)
		}
		
	})
//______________________________________________________________________________删除联程信息
	$(".con").delegate(".removeCon","click",function(){
		$(".connectMess").remove();
	})

	
//______________________________________________________________________________默认排期
	choosePaiQi()
	function choosePaiQi(){
		var schType = $(".scheduleTitle input[type='radio']:checked").val();
		if(schType=="AUTO"){
			$(".scheduleTable_1").hide();
			$(".addSchedule").hide();
			$(".scheduleTable_2").hide();
			$(".scheduleTable_3").show();
		}
	}
	
	
//______________________________________________________________________________排期选择
	$(".asWeek").click(function(){
		$(".scheduleTable_1").show();
		$(".addSchedule").show();
		$(".scheduleTable_2").hide();
		$(".scheduleTable_3").hide();
	})
	$(".asInterval").click(function(){
		$(".scheduleTable_1").hide();
		$(".addSchedule").hide();
		$(".scheduleTable_2").show();
		$(".scheduleTable_3").hide();
	})
	$(".autoRuleInput").click(function(){
		$(".scheduleTable_1").hide();
		$(".addSchedule").hide();
		$(".scheduleTable_2").hide();
		$(".scheduleTable_3").show();
	})

//_______________________________________________________________________________删除排期
	$(".scheduleTable_1").delegate(".deleteSchedule","click",function(){
		if($(".deleteSchedule").length>1){
			$(this).parent().parent().remove();
			countNumber();
		}
	})
	
	
	
	
//_______________________________________________________________________________添加排期
	$(".addSchedule").click(function(){		
		addSchedule();
		countNumber();
	})
//———————————————————————————————————————————————————————————————————————————————添加排期
	function addSchedule(){
		var str="";
		str+="<tr>"+
						"<td class='scheduleNo'></td>"+
						"<td style='width:250px;'>"+
							"<select class='thisDate' style='width:100%;height:100%;border:none;font-family:微软雅黑;padding-left:100px;'>"+
								"<option value=1>星期一</option>"+
								"<option value=2>星期二</option>"+
								"<option value=3>星期三</option>"+
								"<option value=4>星期四</option>"+
								"<option value=5>星期五</option>"+
								"<option value=6>星期六</option>"+
								"<option value=7>星期日</option>"+
							"</select>"+
						"</td>"+
						"<td style='width:250px;'>"+
							"<select class='itval' style='width:100%;height:100%;border:none;font-family:微软雅黑;padding-left:100px;'>"+
								"<option value=0>同周</option>"+
								"<option value=1>下一周</option>"+
								"<option value=2>下二周</option>"+
								"<option value=3>下三周</option>"+
								"<option value=4>下四周</option>"+
								"<option value=5>下五周</option>"+
								"<option value=6>下六周</option>"+
								"<option value=7>下七周</option>"+
							"</select>"+
						"</td>"+
						"<td style='width:250px;'>"+
							"<select class='nextDate' style='width:100%;height:100%;border:none;font-family:微软雅黑;padding-left:100px;'>"+
								"<option value=1>星期一</option>"+
								"<option value=2>星期二</option>"+
								"<option value=3>星期三</option>"+
								"<option value=4>星期四</option>"+
								"<option value=5>星期五</option>"+
								"<option value=6>星期六</option>"+
								"<option value=7>星期日</option>"+
							"</select>"+
						"</td>"+
						"<td><span class='deleteSchedule'>删除</span></td>"+
					"</tr>"
		$(".scheduleTable_1").append(str)
	}
//———————————————————————————————————————————————————————————————————————————————序号计算
	function countNumber(){
		var len = $(".scheduleNo").length;
		for(var i=0;i<len;i++){
			$(".scheduleNo").eq(i).text(i+1)
		}
	}
//———————————————————————————————————————————————————————————————————————————————获取数据后生成table
	function creatTable(){
		var str = "";
		str+="<table>" +
				"<tr class='f868686'>" +
					"<th>航班号</th>" +
					"<th>出发机场</th>" +
					"<th>到达机场</th>" +
					"<th>出发日期</th>" +
					"<th>到达日期</th>" +
					"<th>出发时间</th>" +
					"<th>到达时间</th>" +
					"<th>班期</th>" +
				 "</tr>" +
			 "</table>"+
			 "<p class=showAndHideP><span class=showAndHide>查看更多<span></p>"
		$(".flight_Content").eq(flightTableIndex).append(str)
	}
//———————————————————————————————————————————————————————————————————————————————获取数据后生成添加tr到table
	function creatTr(){
		var str = "";
		str+="<tr class='f60666'>" +
				"<td></td>" +
				"<td></td>" +
				"<td></td>" +
				"<td></td>" +
				"<td></td>" +
				"<td></td>" +
				"<td></td>" +
				"<td></td>" +
				"<td style=display:none; class=postId></td>" +
			 "</tr>"
		$(".flight_Content").eq(flightTableIndex).find("table").append(str)
	}
//———————————————————————————————————————————————————————————————————————————————判断是否需要生成table
	function creatTableOrNot(){
		if($(".flight_Content").eq(flightTableIndex).find("table").length==0){
			creatTable()			
			$(".flight_Content").eq(flightTableIndex).css({"padding-bottom":"20px"})
		}
	}

//———————————————————————————————————————————————————————————————————————————————获取每table中class为postId的td的值
	
	function getFlightNo(){
		var allIdArr = [];
		var tableLength = $(".flight").length;
		for(var i=0;i<tableLength;i++){
			var trLength = $(".flight").eq(i).find("tr").length;
			var idArr = [];
			for(var j=0;j<trLength-1;j++){
				var postId = $(".flight").eq(i).find("tr").find("td[class=postId]").eq(j).text();
				idArr.push(postId)
			}
			allIdArr.push(idArr)
		}
		return allIdArr;
	}
//————————————————————————————————————————————————————————————————————————————————隐藏除了前两个tr外的其他tr
	
	function hideOthersTr(){
		var lengthTable = $(".flightList table").length;
		for(var i=0;i<lengthTable;i++){
			var lengthTr = $(".flightList table").eq(i).find("tr").length;		
			for(var j=2;j<lengthTr;j++){
				$(".flightList table").eq(i).find("tr").eq(j).hide();
			}
		}
	}
	
	
//————————————————————————————————————————————————————————————————————————————————查看更多
	$(".flightList").delegate(".showAndHide","click",function(){			
		var length = $(this).parent().parent().find("table tr").length;
		for(var i=2;i<length;i++){
			$(this).parent().parent().find("table tr").eq(i).toggle();
		}								
		if($(this).text()=="查看更多"){
			$(this).text("收起")
		}else{
			$(this).text("查看更多")
		}
	})
//________________________________________________________________________________删除免票运价
	$(".delFreePricePlan").click(function(){
		$(".freePricePlanNo").text("");
	})





















